package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.work.enums.ButcherReportCertStatus;
import com.jiantong.work.impl.dao.ButcherReportCertDao;
import com.jiantong.work.model.ButcherReportCert;
import com.jiantong.work.service.ButcherReportCertWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by mz on 23/8/17.
 *
 */
@Slf4j
@Service
@RpcProvider
public class ButcherReportCertWriteServiceImpl implements ButcherReportCertWriteService {


    private final ButcherReportCertDao butcherReportCertDao;

    @Autowired
    public ButcherReportCertWriteServiceImpl(ButcherReportCertDao butcherReportCertDao) {
        this.butcherReportCertDao = butcherReportCertDao;
    }

    /**
     * 添加一条
     *
     * @param butcherReportCert 屠宰证对象
     * @return Long
     */
    @Override
    public Response<Long> create(ButcherReportCert butcherReportCert) {
        try{
            butcherReportCertDao.create(butcherReportCert);
            return Response.ok(butcherReportCert.getId());
        }catch (Exception e){
            log.error("failed to add a record , case = {}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to add a record");
        }
    }

    /**
     * 添加多条记录
     *
     * @param list 屠宰证对象
     * @return Integer
     */
    @Override
    public Response<Integer> creates(List<Map<String, Object>> list) {
        try{
            List<ButcherReportCert> butcherReportCerts = buildButcherReportCert(list);
            return Response.ok(butcherReportCertDao.creates(butcherReportCerts));
        }catch (Exception e){
            log.error("failed to add multi records , case = {}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to add multi records");
        }
    }

    private List<ButcherReportCert> buildButcherReportCert(List<Map<String, Object>> list) {

        List<ButcherReportCert> butcherReportCerts = new ArrayList<>();
        ButcherReportCert butcherReportCert = null;
        for (Map<String,Object> map1 : list){
            butcherReportCert = new ButcherReportCert();
            butcherReportCert.setReportId(Long.valueOf((int)map1.get("reportId")));
            butcherReportCert.setReportNumber((String) map1.get("reportNumber"));
            butcherReportCert.setSlaughterQuarantineCertNumber((String) map1.get("slaughterQuarantineCertNumber"));
            butcherReportCert.setSlaughterQuarantineCertPhotoMap((Map<String, Map<String, String>>) map1.get("slaughterQuarantineCertPhotoMap"));
            butcherReportCert.setStatus(ButcherReportCertStatus.HAS_PRINTED.value());
            butcherReportCerts.add(butcherReportCert);
        }

        return butcherReportCerts;
    }


}
